<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$pingset=$this->plugin_setting('ping');
	$member=$this->model('member')->get_member();
	$member['credit1passtime']=date('Y年m月d日',time()+7*24*3600);
	$_SESSION['sex']=$member['sex'];
	$return['member']=$member;
	$post=array();
	$post['num']=1;
	$post['checked']=true;
	$return['post']=$post;
	$return['fee']=$pingset['oneprice'];
	$return['vipbg']=$pingset['vipbg'];
	$return['pingset']=$pingset;
	$return['pingxieyi']=$this->plugin_setting('pingxieyi');
	set_html('title','瓶约购买');
	exi($return);
}
//购买
if($_RQ['op']=='post'){
	$pingset=$this->plugin_setting('ping');
	$postdata=get_postdata();
	if(!$postdata['checked']){
		exi('请先阅读《瓶约协议》','error');
	}
	if(!intval($postdata['num'])){
		exi('请输入购买数量！','error');
	}
	if(!$pingset['oneprice']){
		exi('管理员还未设置瓶约价格！','error');
	}
	$num=$postdata['num'];
	$fee=$num*$pingset['oneprice'];
	//创建一个会员订单
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'num'=>$num,
			'ordersn'=>get_ordersn('jy_xiangqin_pingorderlog','ordersn'),
			'price'=>$fee,
			'money'=>$fee,
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_xiangqin_pingorderlog',$insert);
	$id=pdo_insertid();
	$params=array(
			'fee'=>$fee,
			'title'=>'购买瓶约订单',
			'id'=>$id,
			'plugin'=>'jy_xiangqin',
			'no_type'=>array('hdfk'),
			'back_url'=>'jy_xiangqin/ping.buyping/payresult/',
	);
	$res['pay_id']=cfc('pay')->create_pay_order($params);
	pdo_update('jy_xiangqin_pingorderlog',array('pay_id'=>$res['pay_id']),array('id'=>$id));
	$res['order_id']=$id;
	exi($res);
}
//购买
if($_RQ['op']=='setpay'){
	$pingset=$this->plugin_setting('ping');
	$postdata=get_postdata();
	if(!$postdata['checked']){
		exi('请先阅读《瓶约协议》','error');
	}
	if(!intval($postdata['num'])){
		exi('请输入购买数量！','error');
	}
	if(!$pingset['oneprice']){
		exi('管理员还未设置瓶约价格！','error');
	}
	$num=$postdata['num'];
	$fee=$num*$pingset['oneprice'];
	$id=$_RQ['id'];
	pdo_update('jy_xiangqin_pingorderlog',array('num'=>$num,'price'=>$fee,'money'=>$fee),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$pay_id=pdo_getcolumn('jy_xiangqin_pingorderlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'pay_id');
	pdo_update('core_paylog',array('fee'=>$fee),array('id'=>$pay_id,'uniacid'=>$_SESSION['uniacid']));
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_xiangqin_pingorderlog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_xiangqin_pingorderlog',array('status'=>1,'ispay'=>1,'pay_time'=>time,'paytype'=>$payresult['paytype'],'transid'=>$payresult['transid']),array('id'=>$order['id']));
	pdo_update('jy_xiangqin_member',array('pingnum +='=>$order['num']),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('ping')->up_buyfee($order);
	$this->model('member')->update_agent_level($order['mid']);
	$this->model('fuwu')->create($order);
	$this->model('commission')->jy_teamfee($order);
	$this->model('commission')->jy_area_agent($order);
	$this->model('commission')->jy_commission($order);
	exi('支付成功');
}
?>